package com.aistar.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;

/**
 * Java RSA 加密工具类
 */
public class RSAUtil {
    private static final BigInteger modulus;
    private static final BigInteger privateExponent;
    private static final BigInteger publicExponent;
    private static final String ENCRYPTION_TYPE = "RSA";

    static {
        String modulusStr = "134854070026196432227290524496985030857953423185108551819063656597742216453878574739909062089216622960294953613740214093564366240889276561513472594020580042654299861595306717754325683053186603615753066227342499121788843599284587628691119606081732205698323968799690406554250195037444967825214692891827651875961";
        String privateExponentStr = "51009847810388170879267163621774858705291138757630666640135923936982613575410071681681273924526299390965590431124706766856289410739661045820208212243010493006852693645167743258081769784243579768414064412294230391925463585104952899950580039145363725892906460075487809237845856558727570565874286201243929033313";
        String publicExponentStr = "65537";
        modulus = new BigInteger(modulusStr);
        privateExponent = new BigInteger(privateExponentStr);
        publicExponent = new BigInteger(publicExponentStr);
    }

    // 获取公钥
    public static PublicKey getPublicKey() {
        try {
            KeyFactory keyFactory = KeyFactory.getInstance(ENCRYPTION_TYPE);
            RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, publicExponent);
            return keyFactory.generatePublic(publicKeySpec);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 获取公钥str
    public static String getPublicKeyStr() {
        PublicKey publicKey = getPublicKey();
        if (publicKey != null) {
            byte[] a = publicKey.getEncoded();
            BASE64Encoder base64Encoder = new BASE64Encoder();
            return base64Encoder.encode(a);
        }
        return null;
    }

    // 获取私钥
    public static PrivateKey getPrivateKey() {
        try {
            KeyFactory keyFactory = KeyFactory.getInstance(ENCRYPTION_TYPE);
            RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(modulus, privateExponent);
            return keyFactory.generatePrivate(privateKeySpec);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //公钥加密 str明文
    public static String encrypt(String str, PublicKey publicKey) {
        try {
            Cipher cipher = Cipher.getInstance(ENCRYPTION_TYPE);
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] bytes = cipher.doFinal(str.getBytes());
            BASE64Encoder base64Encoder = new BASE64Encoder();
            return base64Encoder.encode(bytes);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //私钥解密 str密码
    public static String decrypt(String str, PrivateKey privateKey) {
        try {
            Cipher cipher = Cipher.getInstance(ENCRYPTION_TYPE);
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            BASE64Decoder base64Decoder = new BASE64Decoder();
            byte[] inputBytes = base64Decoder.decodeBuffer(str);
            byte[] bytes = cipher.doFinal(inputBytes);
            return new String(bytes);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

//    public static void main(String[] args) throws Exception {
//        PrivateKey privateKey = RSAUtil.getPrivateKey();
//        RSAPublicKey publicKey =(RSAPublicKey) RSAUtil.getPublicKey();
//        System.out.println(publicKey.getModulus());
//        String content = "a1b2c3d4";
//        System.out.println("原文: " + content);
//        String string = RSAUtil.encrypt(content, publicKey);
//        System.out.println("密文: " + string);
//        System.out.println("密文长度: " + string.length());
//        String string2 = RSAUtil.decrypt(string, privateKey);
//        System.out.println("解密: " + string2);
//    }
}
